<?php
namespace app\frontend\controller;
use think\Db;
use think\captcha\Captcha;
use \think\Validate;
use think\Controller;
class Register extends Controller
{
    public function index()
    {
    	return view('./index/register');
    }
     public function check()
    {
    	if ( request()->isPost()) 
        {
          
            $user_phone = input('post.userPhone','','htmlspecialchars');
            $user_pwd = input('post.password');
            $user_repwd = input('post.password1');
            $verify=input('verify');
            $rule = [
                'user_phone'  => 'require|unique:user,user_phone',
                'user_pwd'   => 'require|min:6',
                'user_repwd' => 'require|confirm:user_pwd'
            ];
            $msg = [
                'user_name.require' => '请输入手机号',   
                'user_name.unique' => '此手机号已被注册，请使用其他手机号！',

                'user_pwd.require'  => '请设置登录密码！',
                'user_pwd.min' => '密码长度最小为6位',

                'user_repwd.require'  => '请确认密码！',
                'user_repwd.confirm' => '两次密码不一致'
                
            ];
        
            if(preg_match("/^\d*$/", $user_pwd))
            
                $this->error("密码必须包含字母");//全数字
            
            if(preg_match("/^[a-z]*$/i",$user_pwd))
            
                $this->error( "密码必须包含数字");//全字母
            
            if(!preg_match("/^[a-z\d]*$/i",$user_pwd))
            
                $this->error("密码只能包含数字和字母");//有数字有字母 ";
            if(empty($verify))
            {
            	$this->error("验证码不能为空");
            }
            else if(!captcha_check($verify))
            {
            	$this->error("验证码错误");
            }
            
          
            $data = [
                'user_phone'   =>  $user_phone,
                'user_pwd'    =>  $user_pwd,
                'user_repwd'  =>  $user_repwd
            ];

            $validate = new Validate($rule, $msg);
            $result   = $validate->check($data);
           
            if (!$result ) {
                $this->error($validate->getError());
            }

            $data = [
                'user_phone'   =>  $user_phone,
                'user_pwd' =>  md5($user_pwd)  
            ];
            $ret = db('user')->insert($data);
            if ( $ret == false ) {
                $this->error('注册失败');
            } 
            
            $this->success("注册成功！",Url('/frontend/index/index'));
            
        }

    }
 }